//https://www.nowcoder.com/practice/94a4d381a68b47b7a8bed86f2975db46?tpId=13&tqId=23445&ru=%2Fpractice%2F1c82e8cf713b4bbeb2a5b31cf5b0417c&qru=%2Fta%2Fcoding-interviews%2Fquestion-ranking&sourceUrl=%2Fexam%2Foj%2Fta%3Fpage%3D1%26tpId%3D13%26type%3D13
//思想：金字塔，先算左边，再算右边；时间复杂度o(n);

#include <iostream>
#include <vector>
#include <set>
#include <queue>

using namespace std;

class Solution {
  public:
    vector<int> multiply(const vector<int>& A) {
        if (A.size() <= 1) return {};
        std::vector<int> b_vec(A.size(), 0);

        for (int i = 0; i < b_vec.size(); ++i) {

            int val = 1;
            for (int j = 0; j < A.size(); ++j) {
                if (i == j) {
                    continue;
                }
                val = val * A[j];
            }
            b_vec[i] = val;
        }

        return b_vec;
    }
};

class Solution {
  public:
    vector<int> multiply(const vector<int>& A) {
        //初始化数组B
        vector<int> B(A.size(), 1);
        //先乘左边，从左到右
        for (int i = 1; i < A.size(); i++)
            //每多一位由数组B左边的元素多乘一个前面A的元素
            B[i] = B[i - 1] * A[i - 1];
        int temp = 1;
        //再乘右边，从右到左
        for (int i = A.size() - 1; i >= 0; i--) {
            //temp为右边的累乘
            B[i] *= temp;
            temp *= A[i];
        }
        return B;
    }
};